package cn.xyafu.controller;

import cn.xyafu.po.User;
import cn.xyafu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@Transactional
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 向登录页跳转
     */
    @RequestMapping(value = "/tologin",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }
    /**
     * Login
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String findUserByUser(User user, Model model, HttpSession session){
        User user1 = userService.findUserByUser(user);
//        System.out.println(user1);
        //user != null 登陆成功
        if (user != null){
            session.setAttribute("USER_SESSION",user1);
            return "forward:allType";
        }
        model.addAttribute("msg","用户名或密码错误，请重新登录！");
        return "login";
    }

    /**
     * 向注册页面跳转
     */
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String toRegister(){
        return "register";
    }

    /**
     * register
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public void addUser(User user,String check, HttpServletResponse response,HttpSession session) throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        String checkcode = (String) session.getAttribute("checkcode");
        if (!check.equals(checkcode)){
            response.getWriter().print("<script>alert('验证码错误！');window.location.href='/Lost_ssm/register'</script>");
        }else{
            //        System.out.println("user:"+user);
            Integer i = userService.addUser(user);
//        System.out.println("i:"+i);
            //注册成功
            if (i>0){
                response.getWriter().print("<script>alert('注册成功，请登录！');window.location.href='/Lost_ssm/tologin'</script>");
            }
        }
        response.getWriter().print("<script>alert('网络异常，请重新尝试！');window.location.href='Lost_ssm/register'</script>");
    }
    /**
     * 验证用户名是否存在
     */
    @RequestMapping(value = "/checkuNikeName/{unikename}",method = RequestMethod.GET)
    @ResponseBody
    public String findUserByUnikename(@PathVariable("unikename") String unikename){
        User user = userService.findUserByUnikename(unikename);
//        System.out.println(user);
        //用户存在，返回false，用户名不可用
        if (user != null){
            return "false";
        }
        return "true";
    }

    /**
     * 用户信息修改
     */
    @RequestMapping("/updateUser")
    public void updateUser(User user, HttpSession session, HttpServletResponse response) throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        User user1 = (User) session.getAttribute("USER_SESSION");
        user.setUid(user1.getUid());
        user.setUnikename(user1.getUnikename());
        user.setUpassword(user1.getUpassword());
        this.userService.updateUser(user);
        session.setAttribute("USER_SESSION",this.userService.findUserByUser(user));
        response.getWriter().print("<script>alert('修改成功');window.location.href='my.jsp'</script>");
    }

    /**
     * 用户注销
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "forward:allType";
    }
}
